package com.helloandroid.tools

import org.junit.Test


fun dayStampToLocalDate(str:String):Triple<Int,Int,Int>{
    val ymd = str.split("-").map { it.toInt() }
//    return if(null != ymd && ymd.size >= 3){
//        LocalDate.of(ymd[0],ymd[1],ymd[2])
//    }else{
//        null
//    }
    return if(null != ymd && ymd.size >= 3){
        Triple<Int,Int,Int>(ymd[0],ymd[1],ymd[2])
    }else{
        Triple<Int,Int,Int>(0,0,0)
    }
}

data class MatchTime(val time:String):Comparable<MatchTime>{
    companion object{
        const val Negitive = 1
        const val Positive = -1
    }
    override fun compareTo(other: MatchTime): Int {
        val ld1 = dayStampToLocalDate(time)
        val ld2 = dayStampToLocalDate(other.time)
        return when{
            ld1.first < ld2.first ->  Negitive
            ld1.first > ld2.first ->  Positive
            else -> when{
                ld1.second < ld2.second ->  Negitive
                ld1.second > ld2.second ->  Positive
                else -> when{
                    ld1.third < ld2.third ->  Negitive
                    ld1.third > ld2.third ->  Positive
                    else -> 0
                }
            }
        }
    }
}

class MyTimeUtilsTest {

    @Test
    fun test_dayTimeString(){
        val str1 = MyTimeUtils.dayTimeString(0)
        val str2 = MyTimeUtils.dayTimeString(System.currentTimeMillis())
        println("str1:$str1")
        println("str2:$str2")
    }

    @Test
    fun dayTimeCompare(){
//        val str = "2021-01-02"
//        val ymd = str.split("-").map { it.toInt() }
//        ymd?.let {
//            if(it.size >= 3){
//                val localDate = LocalDate.of(it[0],it[1],it[2])
//            }
//        }

        val str1 = "2021-01-02"
        val str2 = "2021-01-01"
        val str3 = "2021-01-11"
        val str4 = "2020-11-12"
        val str5 = "2021-03-22"
        val str6 = "2021-11-22"
        val str7 = "2021-07-02"
        val arr = arrayListOf<MatchTime>(MatchTime(str1),MatchTime(str2),MatchTime(str3),MatchTime(str4),MatchTime(str5),MatchTime(str6),MatchTime(str7))
        arr.sort()
        println("=============================结果")
        arr.forEach(::println)
    }
}